﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Parking
{
    /// <summary>
    /// Логика взаимодействия для settings.xaml
    /// </summary>
    public partial class settings : Window
    {
        public settings()
        {
                InitializeComponent();
                Fill();
        }

        /// <summary>
        /// Сохранить данные формы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string error = "";
            if (textBox1.Text == "") error = "Не указан логин";
            if (textBox2.Text == "") error = "Не указан пароль";
            if (textBox3.Text == "") error = "Не указан код парковки";
            if (error != "")
            {
                MessageBox.Show(error);
                return;
            }

            //шифруем данные текстбокса
            string login = Crypto.Encrypt(textBox1.Text);
            string password = Crypto.Encrypt(textBox2.Text);
            string parking = Crypto.Encrypt(textBox3.Text);

            //заполняем объект Settings
            Properties.Settings.Default.login = login;
            Properties.Settings.Default.password = password;
            Properties.Settings.Default.parking = parking;

            Properties.Settings.Default.Save();
            Hide();
            MainWindow.MySettingsObject = new MySettings();
        }

        /// <summary>
        /// Заполняем поля формы
        /// </summary>
        private void Fill()
        {
            /*
            string rawLogin = Properties.Settings.Default.login;
            string rawPassword = Properties.Settings.Default.password;
            string login;       //логин после расшифровки
            string password;    //пароль после расшифровки

            if (rawLogin != "") login = Crypto.Decrypt(rawLogin);
            else login = "";

            if (rawPassword != "") password = Crypto.Decrypt(rawPassword);
            else password = "";
            */

            textBox1.Text = MainWindow.MySettingsObject.login;
            textBox2.Text = MainWindow.MySettingsObject.password;
            textBox3.Text = MainWindow.MySettingsObject.parking;
        }

        /// <summary>
        /// Отмена
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Fill();
            Hide();
        }
    }
}
